package Jw20241002;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 一元多项式的加法
 * User: 王圆豪
 * Date: 2024-10-07
 * Time: 23:21
 */

class Num{
    int a;
    int b;
    Num next;

    public Num(){
    }

    public Num(int a, int b){
        this.a = a;
        this.b = b;
    }
}

public class Test3{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n1 = sc.nextInt();
        Num head1 = new Num();
        Num p = head1;
        for(int i = 0;i < n1; i++){
            p.next = new Num(sc.nextInt(), sc.nextInt());
            p = p.next;
        }
        Num head2 = new Num();
        Num q = head2;
        int n2 = sc.nextInt();
        for (int i = 0; i < n2; i++) {
            q.next = new Num(sc.nextInt(), sc.nextInt());
            q = q.next;
        }
        p = head1.next;
        q = head2.next;
        Num head = new Num();
        Num m = head;
        while (q != null && p != null){
            if(p.b > q.b) {
                if (p.a == 0){
                    p = p.next;
                }else {
                    m.next = p;
                    p = p.next;
                    m = m.next;
                }
            }else if(p.b < q.b) {
                if (q.a == 0) {
                    q = q.next;
                }else {
                    m.next = q;
                    q = q.next;
                    m = m.next;
                }
            }else {
                if (p.a + q.a != 0) {
                    m.next = new Num(p.a + q.a, p.b);
                    p = p.next;
                    q = q.next;
                    m = m.next;
                }else {
                    p = p.next;
                    q = q.next;
                }
            }
        }
        if (q != null) {
            m.next = q;
        }
        if (p != null) {
            m.next = p;
        }

        Num o = head.next;
        boolean l = true;
        while (o != null) {
            if (o.a != 0) {
                l = false;
                break;
            }
            o = o.next;
        }
        if (l){
            System.out.print("0 0");
        }else {
            p = head.next;
            System.out.print(p.a + " " + p.b);
            p = p.next;
            while (p != null) {
                System.out.print(" " + p.a + " " + p.b);
                p = p.next;
            }
        }
    }
}